//
//  KDTownController.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/8/11.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDTownController.h"
#import "KDReginType.h"
#import "KDReginDataTool.h"
#import "KDReportController.h"
#import "KDSearchResultController.h"
@interface KDTownController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *infoArr;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation KDTownController
#pragma mark - 懒加载区
- (NSArray *)infoArr{
    if (!_infoArr) {
        NSArray *arr = [KDReginDataTool reginTypeArrWithParentId:self.ReginType.ID];
        
        NSMutableArray *mutableArr = [NSMutableArray arrayWithArray:arr];
        [mutableArr insertObject:self.ReginType atIndex:0];
        _infoArr = [NSArray arrayWithArray:mutableArr];
        if (arr.count == 1) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TownAddressToReportControllerNoty" object:nil userInfo:@{@"address":self.address, @"regionModel":self.ReginType}];
        }
    }
    return _infoArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.address;
    self.view.backgroundColor = BackGroundColor;
    [self setUpTableView];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }

}
- (void)setUpTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(10  , 10, self.view.width -20, self.view.height - 64 -10) style:UITableViewStylePlain];
    tableView.tableFooterView = [UIView new];
    tableView.separatorColor = BackGroundColor;
    self.tableView = tableView;
    //    tableView.editing = YES;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.infoArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId =@"townCell";
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    KDReginType *model = self.infoArr[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KDReginType *model =  self.infoArr[indexPath.row];

    if (indexPath.row != 0) {
        self.address = [NSString stringWithFormat:@"%@  %@", self.address, model.name];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TownAddressToReportControllerNoty" object:nil userInfo:@{@"address":self.address,  @"regionModel":model}];
    UIViewController *VC ;
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[KDBaseRegionController class]] && ![vc isKindOfClass:[KDSearchResultController class]]) {
            VC = vc;break;
        }
    }
    [self.navigationController popToViewController:VC
                                          animated:YES];
}
@end
